// pages/compony-pages/job-way/job-way.js
const app = getApp();
let flag = '';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        jobWayList:[],
        actives:[],
        firstActives:[],
        secondActives:[],
        isShow:false,
        isScroll:false,
        firstList:[],
        secondList:[],
        positionName:''
    },
    /**
     * -------------------网络请求部分-------------------
     */
    getPositionData(){
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        wx.request({
            url: app.globalData.serverAddress.getPositionData,
            data: '',
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function(res) {
                console.log(res.data.BodyContent);
                if(res.statusCode == 200 && res.data.CodeStatus == 20000){
                    let jobWayList = res.data.BodyContent;
                    let actives = [];
                    for (let i = 0; i < jobWayList.length; i++){
                        actives.push(false);
                    }
                    that.setData({
                        actives: actives,
                        jobWayList: jobWayList
                    });
                }
            },
            fail: function (res) { console.log("getPositionData fail" , res)},
            complete: function(res) {wx.hideLoading();},
        })
    },
    /**
     * -------------------网络请求部分-------------------
     */
    myCatchTouch(){
        console.log("-------")
        return;
    },
    clickItem(e){
        if(this.data.lock){
            return;
        }
        this.lockScreen();
        let that = this;
        let index = e.currentTarget.dataset.index;
        let _actives = this.data.actives;
        _actives.forEach(function(item , i){
            _actives[i] = false;
        });
        _actives[index] = true;
        let firstLevelList = that.data.jobWayList[index].SubLevelList;
        let secondList = firstLevelList[0].SubLevelList;
        this.setData({
            actives: _actives,
            isShow:true,
            isAnima:true,
            firstList: firstLevelList,
            secondList:secondList,
            positionName: that.data.jobWayList[index].PositionName
        });
        this.setFirstLeveActive(firstLevelList);
        this.unLockScreen();
    },
    setFirstLeveActive(list){
        let actives = [];
        if(list){
            for(let i = 0; i < list.length; i++){
                if( i == 0){
                    actives.push(true);
                    continue;
                }
                actives.push(false);
            }
            this.setData({
                firstActives:actives
            })
        }
    },
    setSecondLevelActive(list){
        let actives = [];
        if (list) {
            for (let i = 0; i < list.length; i++) {
                actives.push(false);
            }
            this.setData({
                secondActives: actives
            })
        }
    },
    /**
     * ===================事件处理部分===================
     */
    secondClickItem(e){
        let positionName = this.data.positionName;
        let index = e.currentTarget.dataset.index;
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];  //上一个页面
        let obj = this.data.secondList[index];
        wx.navigateBack({
            delta: 1,
            success: function (res) {
                prevPage.setData({
                    jobWayObj: obj,
                    jobType: positionName
                });
                // prevPage.getPositionSkillData(obj.Id);
            }
        });
    },
    lockScreen(){
        this.setData({
            lock:true
        });
    },
    unLockScreen(){
        let that = this;
        setTimeout(function(){
            that.setData({
                lock:false
            })
        } , 800);
    },
    hideClick(){
        if (this.data.lock) {
            return;
        }
        this.lockScreen();
        let that = this;
        this.setData({
            isAnima:false
        });
        setTimeout(function(){
            that.setData({
                isShow:false
            });
        }, 600);
        this.unLockScreen();
    },
    firstClickItem(e){
        let index = e.currentTarget.dataset.index;
        let actives = this.data.firstActives;
        actives.forEach(function (item, i) {
            actives[i] = false;
        });
        actives[index] = true;
        this.setData({
            firstActives: actives
        });
        let secondList = this.data.firstList[index].SubLevelList;
        this.setData({
            secondList:secondList
        });
        // this.setSecondLevelActive(secondList);
    },
    /**
     * ===================事件处理部分===================
     */

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if(options.flag){
            flag = options.flag;
        }
        this.getPositionData();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})